package messageMeClient.manager;

import messageMeClient.view.MainForm;
import messageMeTransfer.connection.ClientConnection;
import messageMeTransfer.interfaces.IPackage;
import messageMeTransfer.utilities.PackageWrapper;
import messageMeUtility.commands.CommandRespond;
import messageMeUtility.interfaces.ICommand;

public class PackageManager extends Thread {

	private MainForm mainForm = null;

	private ClientConnection connection = null;
	private PackageWrapper packageWrapper = null;

	public PackageManager(MainForm mainForm, ClientConnection connection, PackageWrapper wrapper) {
		this.mainForm = mainForm;
		this.connection = connection;
		this.packageWrapper = wrapper;
	}

	public void run() {
		while (true) {
			try {
				IPackage currentPackage = this.connection.recieve();
				if (currentPackage == null)
					continue;

				ICommand currentCommand = this.packageWrapper
						.unwrapPackage(currentPackage);
				if (currentCommand == null)
					continue;

				if (currentCommand instanceof CommandRespond) {
					this.mainForm.appendText(((CommandRespond) currentCommand).getRespondMessage());
				}

			} catch (Exception e) {
			}
		}
	}
}
